package com.kchouse.union;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kchouse.MineFragment;
import com.kchouse.R;
import com.kchouse.common.BaseFragment;
import com.squareup.picasso.Picasso;

/**
 * A simple {@link Fragment} subclass.
 */
public class AgreementFragment extends BaseFragment {


    private  String protocolListstr;
    private View view;
    private LayoutInflater inflater;
    private GridView gv_item;
    private JSONArray protocolList;

    public AgreementFragment() {
        // Required empty public constructor
    }
    @SuppressLint("ValidFragment")
    public AgreementFragment(String protocolListstr) {
        this.protocolListstr=protocolListstr;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            this.inflater = inflater;
            view = inflater.inflate(R.layout.fragment_agreement, null);
            intiData();
            intiView();
        }
        return view;
    }

    private void intiView() {

        gv_item = (GridView) view.findViewById(R.id.gv_item);
        gv_item.setAdapter(new gvAdapter());
    }

    private void intiData() {

         protocolList = JSONObject.parseArray(protocolListstr);
    }

    class gvAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return protocolList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            String imageurl = (String) protocolList.get(position);
            View view = inflater.inflate(R.layout.gv_agreement_item, null);
            TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
            tv_item.setText("协议"+(position+1));
            ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);

            Picasso.with(getActivity()).load(imageurl).error(R.mipmap.ic_loadingerror).into(iv_item);
            return view;
        }
    }


}
